<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    <title>flask | Cucy的博客</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="flask">
    <meta name="description" content="flask">
<meta name="keywords" content="flask">
<meta property="og:type" content="article">
<meta property="og:title" content="flask">
<meta property="og:url" content="http://zhourudong.cn/2016/11/29/12-flask/index.html">
<meta property="og:site_name" content="Cucy的博客">
<meta property="og:description" content="flask">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2017-09-08T07:02:37.038Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="flask">
<meta name="twitter:description" content="flask">
    
        <link rel="alternate" type="application/atom+xml" title="Cucy的博客" href="/atom.xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.6.13">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.png">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">cucy</h5>
          <a href="mailto:292016176@qq.com" title="292016176@qq.com" class="mail">292016176@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/cucy" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">flask</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">flask</h1>
        <h5 class="subtitle">
            
                <time datetime="2016-11-29T02:33:06.000Z" itemprop="datePublished" class="page-time">
  2016-11-29
</time>


            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#flask基本"><span class="post-toc-number">1.</span> <span class="post-toc-text">flask基本</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#模板渲染"><span class="post-toc-number">2.</span> <span class="post-toc-text">模板渲染</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#变量"><span class="post-toc-number">3.</span> <span class="post-toc-text">变量</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#URL参数"><span class="post-toc-number">4.</span> <span class="post-toc-text">URL参数</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#模板渲染-1"><span class="post-toc-number">5.</span> <span class="post-toc-text">模板渲染</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#模板渲染列表"><span class="post-toc-number">5.1.</span> <span class="post-toc-text">模板渲染列表</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#渲染数据结构-for"><span class="post-toc-number">5.2.</span> <span class="post-toc-text">渲染数据结构 for</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#模板中使用条件判断-if"><span class="post-toc-number">5.3.</span> <span class="post-toc-text">模板中使用条件判断 if</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#使用模板宏"><span class="post-toc-number">5.4.</span> <span class="post-toc-text">使用模板宏</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#引用bootstrap到模板"><span class="post-toc-number">5.5.</span> <span class="post-toc-text">引用bootstrap到模板</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#自定义404页面"><span class="post-toc-number">5.6.</span> <span class="post-toc-text">自定义404页面</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#url-for-函数"><span class="post-toc-number">5.7.</span> <span class="post-toc-text">url_for() 函数</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#request-函数"><span class="post-toc-number">6.</span> <span class="post-toc-text">request 函数</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#限定上传长度"><span class="post-toc-number">6.1.</span> <span class="post-toc-text">限定上传长度</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#bootstrap美化表单"><span class="post-toc-number">6.2.</span> <span class="post-toc-text">bootstrap美化表单</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#文件上传"><span class="post-toc-number">6.3.</span> <span class="post-toc-text">文件上传</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#session-信息"><span class="post-toc-number">7.</span> <span class="post-toc-text">session 信息</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#页面访问统计-以及时间"><span class="post-toc-number">7.1.</span> <span class="post-toc-text">页面访问统计 以及时间</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#多种请求-响应方式"><span class="post-toc-number">7.2.</span> <span class="post-toc-text">多种请求 响应方式</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#from表单取值"><span class="post-toc-number">7.3.</span> <span class="post-toc-text">from表单取值</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#flask获取上传表单-列表形式"><span class="post-toc-number">7.4.</span> <span class="post-toc-text">flask获取上传表单(列表形式)</span></a></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-12-flask"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">flask</h1>
        <div class="post-meta">
            <time class="post-time" title="2016-11-29 10:33:06" datetime="2016-11-29T02:33:06.000Z"  itemprop="datePublished">2016-11-29</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>flask<br><a id="more"></a></p>
<h2 id="flask基本"><a href="#flask基本" class="headerlink" title="flask基本"></a>flask基本</h2><p>一个示例：</p>
<figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="comment">#conding:utf8</span></div><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</div><div class="line"></div><div class="line">app = Flask(<span class="string">'todo-list'</span>)</div><div class="line">app.debug = <span class="keyword">True</span></div><div class="line"></div><div class="line"><span class="meta">@app.route('/')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">hello</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">return</span> <span class="string">'hello, world!'</span></div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:</div><div class="line">    app.run(host=<span class="string">'0.0.0.0'</span>,port=<span class="number">8011</span>)</div></pre></td></tr></table></figure>
<h2 id="模板渲染"><a href="#模板渲染" class="headerlink" title="模板渲染"></a>模板渲染</h2><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="comment">#conding:utf8</span></div><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template</div><div class="line"></div><div class="line">app = Flask(<span class="string">'todo-list'</span>)</div><div class="line">app.debug = <span class="keyword">True</span></div><div class="line"></div><div class="line"><span class="meta">@app.route('/')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">hello</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">return</span> render_template(<span class="string">'hello.html'</span>)</div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:</div><div class="line">    app.run(host=<span class="string">'0.0.0.0'</span>,port=<span class="number">8011</span>)</div></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="code"><pre><div class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">H1</span>&gt;</span>Welcome,Jim<span class="tag">&lt;/<span class="name">H1</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span>this is my first page<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure>
<h2 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h2><h2 id="URL参数"><a href="#URL参数" class="headerlink" title="URL参数"></a>URL参数</h2><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</div><div class="line">app = Flask(__name__)</div><div class="line"></div><div class="line"><span class="meta">@app.route('/user/&lt;name&gt;')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">user</span><span class="params">(name)</span>:</span></div><div class="line">    <span class="keyword">return</span> <span class="string">'&lt;h1&gt;Hello, &#123;0&#125;!&lt;/h1&gt;'</span>.format(name)</div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    app.run(debug=<span class="keyword">True</span>)</div></pre></td></tr></table></figure>
<h2 id="模板渲染-1"><a href="#模板渲染-1" class="headerlink" title="模板渲染"></a>模板渲染</h2><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template</div><div class="line">app = Flask(__name__)</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>)</div></pre></td></tr></table></figure>
<h3 id="模板渲染列表"><a href="#模板渲染列表" class="headerlink" title="模板渲染列表"></a>模板渲染列表</h3><figure class="highlight python"><table><tr><td class="code"><pre><div class="line">├── templates</div><div class="line">│   └── index.html</div><div class="line">└── weather.py</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment"># 模板中元素的渲染列表</span></div><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template</div><div class="line">app = Flask(__name__)</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></div><div class="line">    months = [<span class="string">'January'</span>, <span class="string">'February'</span>, <span class="string">'March'</span>, <span class="string">'April'</span>, <span class="string">'May'</span>, <span class="string">'June'</span>, <span class="string">'July'</span>,</div><div class="line">              <span class="string">'August'</span>, <span class="string">'September'</span>, <span class="string">'October'</span>, <span class="string">'November'</span>, <span class="string">'December'</span>]</div><div class="line">    <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>, city=<span class="string">'Portland, OR'</span>, months=months)</div></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>Weather Averages<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123; city &#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">hr</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">table</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">th</span>&gt;</span>Month<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">th</span>&gt;</span>Min<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">th</span>&gt;</span>Max<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">th</span>&gt;</span>Rainfall<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></div><div class="line">    &#123;% for month in months %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">td</span>&gt;</span>&#123;&#123; month &#125;&#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">td</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">td</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">td</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></div><div class="line">    &#123;% endfor %&#125;</div><div class="line"><span class="tag">&lt;/<span class="name">table</span>&gt;</span></div></pre></td></tr></table></figure>
<h3 id="渲染数据结构-for"><a href="#渲染数据结构-for" class="headerlink" title="渲染数据结构 for"></a>渲染数据结构 for</h3><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="comment">#coding:utf-8</span></div><div class="line"></div><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template</div><div class="line">app = Flask(__name__)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></div><div class="line">	city = <span class="string">u'北京'</span></div><div class="line">	months = [<span class="string">'1'</span>, <span class="string">'2'</span>, <span class="string">'3'</span>, <span class="string">'4'</span>,<span class="string">'5'</span>, <span class="string">'6'</span>, <span class="string">'7'</span>, <span class="string">'8'</span>, <span class="string">'9'</span>, <span class="string">'10'</span>, <span class="string">'11'</span>, <span class="string">'12'</span>]</div><div class="line">	weather = &#123;</div><div class="line">        <span class="string">'1'</span>: &#123;<span class="string">'min'</span>: <span class="number">38</span>, <span class="string">'max'</span>: <span class="number">47</span>, <span class="string">'rain'</span>: <span class="number">6.14</span>&#125;,</div><div class="line">        <span class="string">'2'</span>: &#123;<span class="string">'min'</span>: <span class="number">38</span>, <span class="string">'max'</span>: <span class="number">51</span>, <span class="string">'rain'</span>: <span class="number">4.79</span>&#125;,</div><div class="line">        <span class="string">'3'</span>: &#123;<span class="string">'min'</span>: <span class="number">41</span>, <span class="string">'max'</span>: <span class="number">56</span>, <span class="string">'rain'</span>: <span class="number">4.5</span>&#125;,</div><div class="line">        <span class="string">'4'</span>: &#123;<span class="string">'min'</span>: <span class="number">44</span>, <span class="string">'max'</span>: <span class="number">61</span>, <span class="string">'rain'</span>: <span class="number">3.4</span>&#125;,</div><div class="line">        <span class="string">'5'</span>: &#123;<span class="string">'min'</span>: <span class="number">49</span>, <span class="string">'max'</span>: <span class="number">67</span>, <span class="string">'rain'</span>: <span class="number">2.55</span>&#125;,</div><div class="line">        <span class="string">'6'</span>: &#123;<span class="string">'min'</span>: <span class="number">53</span>, <span class="string">'max'</span>: <span class="number">73</span>, <span class="string">'rain'</span>: <span class="number">1.69</span>&#125;,</div><div class="line">        <span class="string">'7'</span>: &#123;<span class="string">'min'</span>: <span class="number">57</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">0.59</span>&#125;,</div><div class="line">        <span class="string">'8'</span>: &#123;<span class="string">'min'</span>: <span class="number">58</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">0.71</span>&#125;,</div><div class="line">        <span class="string">'9'</span>: &#123;<span class="string">'min'</span>: <span class="number">54</span>, <span class="string">'max'</span>: <span class="number">75</span>, <span class="string">'rain'</span>: <span class="number">1.54</span>&#125;,</div><div class="line">        <span class="string">'10'</span>: &#123;<span class="string">'min'</span>: <span class="number">48</span>, <span class="string">'max'</span>: <span class="number">63</span>, <span class="string">'rain'</span>: <span class="number">3.42</span>&#125;,</div><div class="line">        <span class="string">'11'</span>: &#123;<span class="string">'min'</span>: <span class="number">41</span>, <span class="string">'max'</span>: <span class="number">52</span>, <span class="string">'rain'</span>: <span class="number">6.74</span>&#125;,</div><div class="line">        <span class="string">'12'</span>: &#123;<span class="string">'min'</span>: <span class="number">36</span>, <span class="string">'max'</span>: <span class="number">45</span>, <span class="string">'rain'</span>: <span class="number">6.94</span>&#125;</div><div class="line">    &#125;</div><div class="line">	<span class="keyword">return</span> render_template(<span class="string">'index.html'</span>, city=city, months=months,weather=weather)</div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    app.run(debug=<span class="keyword">True</span>)</div></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="code"><pre><div class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">title</span>&gt;</span>sddsddd<span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">h1</span>&gt;</span>天气平均值<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;city&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">table</span> <span class="attr">border</span>=<span class="string">"1px"</span>&gt;</span></div><div class="line">		<span class="tag">&lt;<span class="name">tr</span>&gt;</span></div><div class="line">			<span class="tag">&lt;<span class="name">th</span>&gt;</span>月份<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">			<span class="tag">&lt;<span class="name">th</span>&gt;</span>最大<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">			<span class="tag">&lt;<span class="name">th</span>&gt;</span>最小<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">			<span class="tag">&lt;<span class="name">th</span>&gt;</span>平均值<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">		<span class="tag">&lt;/<span class="name">tr</span>&gt;</span></div><div class="line">		&#123;% for month in months %&#125;</div><div class="line">		<span class="tag">&lt;<span class="name">tr</span>&gt;</span></div><div class="line">	        <span class="tag">&lt;<span class="name">td</span>&gt;</span>&#123;&#123; month &#125;&#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">	        <span class="tag">&lt;<span class="name">td</span>&gt;</span>&#123;&#123;weather[month]['max']&#125;&#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">	        <span class="tag">&lt;<span class="name">td</span>&gt;</span>&#123;&#123;weather[month]['min']&#125;&#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">	        <span class="tag">&lt;<span class="name">td</span>&gt;</span>&#123;&#123;weather[month]['rain']&#125;&#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">    	<span class="tag">&lt;/<span class="name">tr</span>&gt;</span></div><div class="line">		&#123;% endfor%&#125;</div><div class="line">	<span class="tag">&lt;/<span class="name">table</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure>
<h3 id="模板中使用条件判断-if"><a href="#模板中使用条件判断-if" class="headerlink" title="模板中使用条件判断 if"></a>模板中使用条件判断 if</h3><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="comment">#coding:utf-8</span></div><div class="line"></div><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template</div><div class="line">app = Flask(__name__)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></div><div class="line">	city = <span class="string">u'北京'</span></div><div class="line">	months = [<span class="string">'1'</span>, <span class="string">'2'</span>, <span class="string">'3'</span>, <span class="string">'4'</span>,<span class="string">'5'</span>, <span class="string">'6'</span>, <span class="string">'7'</span>, <span class="string">'8'</span>, <span class="string">'9'</span>, <span class="string">'10'</span>, <span class="string">'11'</span>, <span class="string">'12'</span>]</div><div class="line">	weather = &#123;</div><div class="line">        <span class="string">'1'</span>: &#123;<span class="string">'min'</span>: <span class="number">38</span>, <span class="string">'max'</span>: <span class="number">47</span>, <span class="string">'rain'</span>: <span class="number">6.14</span>&#125;,</div><div class="line">        <span class="string">'2'</span>: &#123;<span class="string">'min'</span>: <span class="number">38</span>, <span class="string">'max'</span>: <span class="number">51</span>, <span class="string">'rain'</span>: <span class="number">4.79</span>&#125;,</div><div class="line">        <span class="string">'3'</span>: &#123;<span class="string">'min'</span>: <span class="number">41</span>, <span class="string">'max'</span>: <span class="number">56</span>, <span class="string">'rain'</span>: <span class="number">4.5</span>&#125;,</div><div class="line">        <span class="string">'4'</span>: &#123;<span class="string">'min'</span>: <span class="number">44</span>, <span class="string">'max'</span>: <span class="number">61</span>, <span class="string">'rain'</span>: <span class="number">3.4</span>&#125;,</div><div class="line">        <span class="string">'5'</span>: &#123;<span class="string">'min'</span>: <span class="number">49</span>, <span class="string">'max'</span>: <span class="number">67</span>, <span class="string">'rain'</span>: <span class="number">2.55</span>&#125;,</div><div class="line">        <span class="string">'6'</span>: &#123;<span class="string">'min'</span>: <span class="number">53</span>, <span class="string">'max'</span>: <span class="number">73</span>, <span class="string">'rain'</span>: <span class="number">1.69</span>&#125;,</div><div class="line">        <span class="string">'7'</span>: &#123;<span class="string">'min'</span>: <span class="number">57</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">0.59</span>&#125;,</div><div class="line">        <span class="string">'8'</span>: &#123;<span class="string">'min'</span>: <span class="number">58</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">0.71</span>&#125;,</div><div class="line">        <span class="string">'9'</span>: &#123;<span class="string">'min'</span>: <span class="number">54</span>, <span class="string">'max'</span>: <span class="number">75</span>, <span class="string">'rain'</span>: <span class="number">1.54</span>&#125;,</div><div class="line">        <span class="string">'10'</span>: &#123;<span class="string">'min'</span>: <span class="number">48</span>, <span class="string">'max'</span>: <span class="number">63</span>, <span class="string">'rain'</span>: <span class="number">3.42</span>&#125;,</div><div class="line">        <span class="string">'11'</span>: &#123;<span class="string">'min'</span>: <span class="number">41</span>, <span class="string">'max'</span>: <span class="number">52</span>, <span class="string">'rain'</span>: <span class="number">6.74</span>&#125;,</div><div class="line">        <span class="string">'12'</span>: &#123;<span class="string">'min'</span>: <span class="number">36</span>, <span class="string">'max'</span>: <span class="number">45</span>, <span class="string">'rain'</span>: <span class="number">6.94</span>&#125;</div><div class="line">    &#125;</div><div class="line">	highlight = &#123;<span class="string">'min'</span>: <span class="number">40</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">5</span>&#125;</div><div class="line"></div><div class="line">	<span class="keyword">return</span> render_template(<span class="string">'index.html'</span>, city=city, months=months,weather=weather,</div><div class="line">		highlight=highlight)</div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    app.run(debug=<span class="keyword">True</span>)</div></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="code"><pre><div class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">title</span>&gt;</span>sddsddd<span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">h1</span>&gt;</span>天气平均值<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;city&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">table</span> <span class="attr">border</span>=<span class="string">"1px"</span>&gt;</span></div><div class="line">		<span class="tag">&lt;<span class="name">tr</span>&gt;</span></div><div class="line">			<span class="tag">&lt;<span class="name">th</span>&gt;</span>月份<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">			<span class="tag">&lt;<span class="name">th</span>&gt;</span>最大<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">			<span class="tag">&lt;<span class="name">th</span>&gt;</span>最小<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">			<span class="tag">&lt;<span class="name">th</span>&gt;</span>平均值<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">		<span class="tag">&lt;/<span class="name">tr</span>&gt;</span></div><div class="line">		&#123;% for month in months %&#125;</div><div class="line">		<span class="tag">&lt;<span class="name">tr</span>&gt;</span></div><div class="line">	        <span class="tag">&lt;<span class="name">td</span>&gt;</span>&#123;&#123; month &#125;&#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">	     <span class="tag">&lt;<span class="name">td</span>&gt;</span></div><div class="line">	     <span class="comment">&lt;!-- 如果 每个月的值满足表达式 小于等于 则对其加粗显示 --&gt;</span></div><div class="line">            &#123;% set hl = weather[month]['min'] <span class="tag">&lt;<span class="name">=</span> <span class="attr">highlight</span>['<span class="attr">min</span>'] %&#125;</span></div><div class="line">            &#123;% if hl %&#125;&lt;b&gt;&#123;% endif %&#125;</div><div class="line">            &#123;&#123; weather[month]['min'] &#125;&#125;</div><div class="line">            &#123;% if hl %&#125;<span class="tag">&lt;/<span class="name">b</span>&gt;</span>&#123;% endif %&#125;</div><div class="line">        <span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">td</span>&gt;</span></div><div class="line">            &#123;% set hl = weather[month]['max'] &gt;= highlight['max'] %&#125;</div><div class="line">            &#123;% if hl %&#125;<span class="tag">&lt;<span class="name">b</span>&gt;</span>&#123;% endif %&#125;</div><div class="line">            &#123;&#123; weather[month]['max'] &#125;&#125;</div><div class="line">            &#123;% if hl %&#125;<span class="tag">&lt;/<span class="name">b</span>&gt;</span>&#123;% endif %&#125;</div><div class="line">        <span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">td</span>&gt;</span></div><div class="line">            &#123;% set hl = weather[month]['rain'] &gt;= highlight['rain'] %&#125;</div><div class="line">            &#123;% if hl %&#125;<span class="tag">&lt;<span class="name">b</span>&gt;</span>&#123;% endif %&#125;</div><div class="line">            &#123;&#123; weather[month]['rain'] &#125;&#125;</div><div class="line">            &#123;% if hl %&#125;<span class="tag">&lt;/<span class="name">b</span>&gt;</span>&#123;% endif %&#125;</div><div class="line">            <span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">    	<span class="tag">&lt;/<span class="name">tr</span>&gt;</span></div><div class="line">		&#123;% endfor%&#125;</div><div class="line">	<span class="tag">&lt;/<span class="name">table</span>&gt;</span></div><div class="line"></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure>
<h3 id="使用模板宏"><a href="#使用模板宏" class="headerlink" title="使用模板宏"></a>使用模板宏</h3><p>相当于py中的函数，函数再调用函数减少代码冗余量</p>
<figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="comment">#coding:utf-8</span></div><div class="line"></div><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template</div><div class="line">app = Flask(__name__)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></div><div class="line">	city = <span class="string">u'北京'</span></div><div class="line">	months = [<span class="string">'1'</span>, <span class="string">'2'</span>, <span class="string">'3'</span>, <span class="string">'4'</span>,<span class="string">'5'</span>, <span class="string">'6'</span>, <span class="string">'7'</span>, <span class="string">'8'</span>, <span class="string">'9'</span>, <span class="string">'10'</span>, <span class="string">'11'</span>, <span class="string">'12'</span>]</div><div class="line">	weather = &#123;</div><div class="line">        <span class="string">'1'</span>: &#123;<span class="string">'min'</span>: <span class="number">38</span>, <span class="string">'max'</span>: <span class="number">47</span>, <span class="string">'rain'</span>: <span class="number">6.14</span>&#125;,</div><div class="line">        <span class="string">'2'</span>: &#123;<span class="string">'min'</span>: <span class="number">38</span>, <span class="string">'max'</span>: <span class="number">51</span>, <span class="string">'rain'</span>: <span class="number">4.79</span>&#125;,</div><div class="line">        <span class="string">'3'</span>: &#123;<span class="string">'min'</span>: <span class="number">41</span>, <span class="string">'max'</span>: <span class="number">56</span>, <span class="string">'rain'</span>: <span class="number">4.5</span>&#125;,</div><div class="line">        <span class="string">'4'</span>: &#123;<span class="string">'min'</span>: <span class="number">44</span>, <span class="string">'max'</span>: <span class="number">61</span>, <span class="string">'rain'</span>: <span class="number">3.4</span>&#125;,</div><div class="line">        <span class="string">'5'</span>: &#123;<span class="string">'min'</span>: <span class="number">49</span>, <span class="string">'max'</span>: <span class="number">67</span>, <span class="string">'rain'</span>: <span class="number">2.55</span>&#125;,</div><div class="line">        <span class="string">'6'</span>: &#123;<span class="string">'min'</span>: <span class="number">53</span>, <span class="string">'max'</span>: <span class="number">73</span>, <span class="string">'rain'</span>: <span class="number">1.69</span>&#125;,</div><div class="line">        <span class="string">'7'</span>: &#123;<span class="string">'min'</span>: <span class="number">57</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">0.59</span>&#125;,</div><div class="line">        <span class="string">'8'</span>: &#123;<span class="string">'min'</span>: <span class="number">58</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">0.71</span>&#125;,</div><div class="line">        <span class="string">'9'</span>: &#123;<span class="string">'min'</span>: <span class="number">54</span>, <span class="string">'max'</span>: <span class="number">75</span>, <span class="string">'rain'</span>: <span class="number">1.54</span>&#125;,</div><div class="line">        <span class="string">'10'</span>: &#123;<span class="string">'min'</span>: <span class="number">48</span>, <span class="string">'max'</span>: <span class="number">63</span>, <span class="string">'rain'</span>: <span class="number">3.42</span>&#125;,</div><div class="line">        <span class="string">'11'</span>: &#123;<span class="string">'min'</span>: <span class="number">41</span>, <span class="string">'max'</span>: <span class="number">52</span>, <span class="string">'rain'</span>: <span class="number">6.74</span>&#125;,</div><div class="line">        <span class="string">'12'</span>: &#123;<span class="string">'min'</span>: <span class="number">36</span>, <span class="string">'max'</span>: <span class="number">45</span>, <span class="string">'rain'</span>: <span class="number">6.94</span>&#125;</div><div class="line">    &#125;</div><div class="line">	highlight = &#123;<span class="string">'min'</span>: <span class="number">40</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">5</span>&#125;</div><div class="line"></div><div class="line">	<span class="keyword">return</span> render_template(<span class="string">'index.html'</span>, city=city, months=months,</div><div class="line">		weather=weather,highlight=highlight)</div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    app.run(debug=<span class="keyword">True</span>)</div></pre></td></tr></table></figure>
<p>index.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line">&#123;% import 'macros.html' as macros %&#125;</div><div class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">title</span>&gt;</span>sddsddd<span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">h1</span>&gt;</span>天气平均值<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;city&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">table</span> <span class="attr">border</span>=<span class="string">"1px"</span>&gt;</span></div><div class="line">		<span class="tag">&lt;<span class="name">tr</span>&gt;</span></div><div class="line">			<span class="tag">&lt;<span class="name">th</span>&gt;</span>月份<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">			<span class="tag">&lt;<span class="name">th</span>&gt;</span>最小<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">			<span class="tag">&lt;<span class="name">th</span>&gt;</span>最大<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">			<span class="tag">&lt;<span class="name">th</span>&gt;</span>降雨量<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">		<span class="tag">&lt;/<span class="name">tr</span>&gt;</span></div><div class="line"></div><div class="line">		&#123;% for month in months %&#125;</div><div class="line">			&#123;&#123; macros.weather_row(month,</div><div class="line">							weather[month]['min'],</div><div class="line">							weather[month]['max'],</div><div class="line">							weather[month]['rain'],</div><div class="line">							highlight['min'],</div><div class="line">							highlight['max'],</div><div class="line">							highlight['rain'] </div><div class="line">							) </div><div class="line">			&#125;&#125;	</div><div class="line">		&#123;% endfor%&#125;</div><div class="line">	<span class="tag">&lt;/<span class="name">table</span>&gt;</span></div><div class="line"></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>macros.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 定义函数 值与变量 当h1满足条件时加粗字体 --&gt;</span></div><div class="line">&#123;% macro weather_cell(value, hl) %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">td</span>&gt;</span></div><div class="line">        &#123;% if hl %&#125;<span class="tag">&lt;<span class="name">b</span>&gt;</span>&#123;% endif %&#125;</div><div class="line">        &#123;&#123; value &#125;&#125;</div><div class="line">        &#123;% if hl %&#125;<span class="tag">&lt;/<span class="name">b</span>&gt;</span>&#123;% endif %&#125;</div><div class="line">    <span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">&#123;% endmacro %&#125;</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">&#123;% macro weather_row(month, min, max, rain, hl_min, hl_max, hl_rain) %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">td</span>&gt;</span>&#123;&#123; month &#125;&#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">        &#123;&#123; weather_cell(min, min <span class="tag">&lt;<span class="name">=</span> <span class="attr">hl_min</span>) &#125;&#125;</span></div><div class="line"><span class="tag">        &#123;&#123; <span class="attr">weather_cell</span>(<span class="attr">max</span>, <span class="attr">max</span> &gt;</span>= hl_max) &#125;&#125;</div><div class="line">        &#123;&#123; weather_cell(rain, rain &gt;= hl_rain) &#125;&#125;</div><div class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></div><div class="line">&#123;% endmacro %&#125;</div><div class="line"><span class="comment">&lt;!-- 函数调用函数  当min 低于预定值时会加粗显示--&gt;</span></div></pre></td></tr></table></figure></p>
<h3 id="引用bootstrap到模板"><a href="#引用bootstrap到模板" class="headerlink" title="引用bootstrap到模板"></a>引用bootstrap到模板</h3><p>pip install flask-bootstrap<br>app.py<br><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="comment">#coding:utf-8</span></div><div class="line"></div><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template</div><div class="line"><span class="keyword">from</span> flask_bootstrap <span class="keyword">import</span> Bootstrap</div><div class="line">app = Flask(__name__)</div><div class="line">bootstrap = Bootstrap(app)</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></div><div class="line">	city = <span class="string">u'北京'</span></div><div class="line">	months = [<span class="string">'1'</span>, <span class="string">'2'</span>, <span class="string">'3'</span>, <span class="string">'4'</span>,<span class="string">'5'</span>, <span class="string">'6'</span>, <span class="string">'7'</span>, <span class="string">'8'</span>, <span class="string">'9'</span>, <span class="string">'10'</span>, <span class="string">'11'</span>, <span class="string">'12'</span>]</div><div class="line">	weather = &#123;</div><div class="line">        <span class="string">'1'</span>: &#123;<span class="string">'min'</span>: <span class="number">38</span>, <span class="string">'max'</span>: <span class="number">47</span>, <span class="string">'rain'</span>: <span class="number">6.14</span>&#125;,</div><div class="line">        <span class="string">'2'</span>: &#123;<span class="string">'min'</span>: <span class="number">38</span>, <span class="string">'max'</span>: <span class="number">51</span>, <span class="string">'rain'</span>: <span class="number">4.79</span>&#125;,</div><div class="line">        <span class="string">'3'</span>: &#123;<span class="string">'min'</span>: <span class="number">41</span>, <span class="string">'max'</span>: <span class="number">56</span>, <span class="string">'rain'</span>: <span class="number">4.5</span>&#125;,</div><div class="line">        <span class="string">'4'</span>: &#123;<span class="string">'min'</span>: <span class="number">44</span>, <span class="string">'max'</span>: <span class="number">61</span>, <span class="string">'rain'</span>: <span class="number">3.4</span>&#125;,</div><div class="line">        <span class="string">'5'</span>: &#123;<span class="string">'min'</span>: <span class="number">49</span>, <span class="string">'max'</span>: <span class="number">67</span>, <span class="string">'rain'</span>: <span class="number">2.55</span>&#125;,</div><div class="line">        <span class="string">'6'</span>: &#123;<span class="string">'min'</span>: <span class="number">53</span>, <span class="string">'max'</span>: <span class="number">73</span>, <span class="string">'rain'</span>: <span class="number">1.69</span>&#125;,</div><div class="line">        <span class="string">'7'</span>: &#123;<span class="string">'min'</span>: <span class="number">57</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">0.59</span>&#125;,</div><div class="line">        <span class="string">'8'</span>: &#123;<span class="string">'min'</span>: <span class="number">58</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">0.71</span>&#125;,</div><div class="line">        <span class="string">'9'</span>: &#123;<span class="string">'min'</span>: <span class="number">54</span>, <span class="string">'max'</span>: <span class="number">75</span>, <span class="string">'rain'</span>: <span class="number">1.54</span>&#125;,</div><div class="line">        <span class="string">'10'</span>: &#123;<span class="string">'min'</span>: <span class="number">48</span>, <span class="string">'max'</span>: <span class="number">63</span>, <span class="string">'rain'</span>: <span class="number">3.42</span>&#125;,</div><div class="line">        <span class="string">'11'</span>: &#123;<span class="string">'min'</span>: <span class="number">41</span>, <span class="string">'max'</span>: <span class="number">52</span>, <span class="string">'rain'</span>: <span class="number">6.74</span>&#125;,</div><div class="line">        <span class="string">'12'</span>: &#123;<span class="string">'min'</span>: <span class="number">36</span>, <span class="string">'max'</span>: <span class="number">45</span>, <span class="string">'rain'</span>: <span class="number">6.94</span>&#125;</div><div class="line">    &#125;</div><div class="line">	highlight = &#123;<span class="string">'min'</span>: <span class="number">40</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">5</span>&#125;</div><div class="line"></div><div class="line">	<span class="keyword">return</span> render_template(<span class="string">'index.html'</span>, city=city, months=months,</div><div class="line">		weather=weather,highlight=highlight)</div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    app.run(port=<span class="number">1012</span>, debug=<span class="keyword">True</span>)</div></pre></td></tr></table></figure></p>
<figure class="highlight html"><table><tr><td class="code"><pre><div class="line">&#123;% extends 'bootstrap/base.html' %&#125;</div><div class="line">&#123;% import 'macros.html' as macros %&#125;</div><div class="line"></div><div class="line">&#123;% block title %&#125;Weather Averages for &#123;&#123; city &#125;&#125;&#123;% endblock %&#125;</div><div class="line"></div><div class="line">&#123;% block navbar %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">nav</span> <span class="attr">class</span>=<span class="string">"navbar navbar-inverse"</span> <span class="attr">role</span>=<span class="string">"navigation"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">"navbar-brand"</span> <span class="attr">href</span>=<span class="string">"#"</span>&gt;</span>Weather<span class="tag">&lt;/<span class="name">a</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span>    </div><div class="line">    <span class="tag">&lt;/<span class="name">nav</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div><div class="line"></div><div class="line">&#123;% block content %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Weather Averages<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123; city &#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">table</span> <span class="attr">class</span>=<span class="string">"table table-hover"</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">tr</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">th</span>&gt;</span>Month<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">th</span>&gt;</span>Min<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">th</span>&gt;</span>Max<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">th</span>&gt;</span>Rainfall<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">            <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></div><div class="line">            &#123;% for month in months %&#125;</div><div class="line">                &#123;&#123; macros.weather_row(month,</div><div class="line">                                      weather[month]['min'],</div><div class="line">                                      weather[month]['max'], </div><div class="line">                                      weather[month]['rain'],</div><div class="line">                                      highlight['min'],</div><div class="line">                                      highlight['max'],</div><div class="line">                                      highlight['rain']) &#125;&#125;</div><div class="line">            &#123;% endfor %&#125;</div><div class="line">        <span class="tag">&lt;/<span class="name">table</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span>    </div><div class="line">&#123;% endblock %&#125;</div></pre></td></tr></table></figure>
<p>macros.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 定义函数 值与变量 当h1满足条件时加粗字体 --&gt;</span></div><div class="line">&#123;% macro weather_cell(value, hl) %&#125;</div><div class="line">   <span class="tag">&lt;<span class="name">td&#123;%</span> <span class="attr">if</span> <span class="attr">hl</span> %&#125; <span class="attr">class</span>=<span class="string">"warning"</span>&#123;% <span class="attr">endif</span> %&#125;&gt;</span>&#123;&#123; value &#125;&#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">&#123;% endmacro %&#125;</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">&#123;% macro weather_row(month, min, max, rain, hl_min, hl_max, hl_rain) %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">td</span>&gt;</span>&#123;&#123; month &#125;&#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">        &#123;&#123; weather_cell(min, min <span class="tag">&lt;<span class="name">=</span> <span class="attr">hl_min</span>) &#125;&#125;</span></div><div class="line"><span class="tag">        &#123;&#123; <span class="attr">weather_cell</span>(<span class="attr">max</span>, <span class="attr">max</span> &gt;</span>= hl_max) &#125;&#125;</div><div class="line">        &#123;&#123; weather_cell(rain, rain &gt;= hl_rain) &#125;&#125;</div><div class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></div><div class="line">&#123;% endmacro %&#125;</div><div class="line"><span class="comment">&lt;!-- 函数调用函数  当min 低于预定值时会加粗显示--&gt;</span></div><div class="line"></div><div class="line"></div><div class="line">&#123;% macro weather_cell(value, hl) %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">td&#123;%</span> <span class="attr">if</span> <span class="attr">hl</span> %&#125; <span class="attr">class</span>=<span class="string">"warning"</span>&#123;% <span class="attr">endif</span> %&#125;&gt;</span>&#123;&#123; value &#125;&#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">&#123;% endmacro %&#125;</div></pre></td></tr></table></figure></p>
<h3 id="自定义404页面"><a href="#自定义404页面" class="headerlink" title="自定义404页面"></a>自定义404页面</h3><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="comment">#coding:utf-8</span></div><div class="line"></div><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template</div><div class="line"><span class="keyword">from</span> flask_bootstrap <span class="keyword">import</span> Bootstrap</div><div class="line">app = Flask(__name__)</div><div class="line">bootstrap = Bootstrap(app)</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></div><div class="line">	city = <span class="string">u'北京'</span></div><div class="line">	months = [<span class="string">'1'</span>, <span class="string">'2'</span>, <span class="string">'3'</span>, <span class="string">'4'</span>,<span class="string">'5'</span>, <span class="string">'6'</span>, <span class="string">'7'</span>, <span class="string">'8'</span>, <span class="string">'9'</span>, <span class="string">'10'</span>, <span class="string">'11'</span>, <span class="string">'12'</span>]</div><div class="line">	weather = &#123;</div><div class="line">        <span class="string">'1'</span>: &#123;<span class="string">'min'</span>: <span class="number">38</span>, <span class="string">'max'</span>: <span class="number">47</span>, <span class="string">'rain'</span>: <span class="number">6.14</span>&#125;,</div><div class="line">        <span class="string">'2'</span>: &#123;<span class="string">'min'</span>: <span class="number">38</span>, <span class="string">'max'</span>: <span class="number">51</span>, <span class="string">'rain'</span>: <span class="number">4.79</span>&#125;,</div><div class="line">        <span class="string">'3'</span>: &#123;<span class="string">'min'</span>: <span class="number">41</span>, <span class="string">'max'</span>: <span class="number">56</span>, <span class="string">'rain'</span>: <span class="number">4.5</span>&#125;,</div><div class="line">        <span class="string">'4'</span>: &#123;<span class="string">'min'</span>: <span class="number">44</span>, <span class="string">'max'</span>: <span class="number">61</span>, <span class="string">'rain'</span>: <span class="number">3.4</span>&#125;,</div><div class="line">        <span class="string">'5'</span>: &#123;<span class="string">'min'</span>: <span class="number">49</span>, <span class="string">'max'</span>: <span class="number">67</span>, <span class="string">'rain'</span>: <span class="number">2.55</span>&#125;,</div><div class="line">        <span class="string">'6'</span>: &#123;<span class="string">'min'</span>: <span class="number">53</span>, <span class="string">'max'</span>: <span class="number">73</span>, <span class="string">'rain'</span>: <span class="number">1.69</span>&#125;,</div><div class="line">        <span class="string">'7'</span>: &#123;<span class="string">'min'</span>: <span class="number">57</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">0.59</span>&#125;,</div><div class="line">        <span class="string">'8'</span>: &#123;<span class="string">'min'</span>: <span class="number">58</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">0.71</span>&#125;,</div><div class="line">        <span class="string">'9'</span>: &#123;<span class="string">'min'</span>: <span class="number">54</span>, <span class="string">'max'</span>: <span class="number">75</span>, <span class="string">'rain'</span>: <span class="number">1.54</span>&#125;,</div><div class="line">        <span class="string">'10'</span>: &#123;<span class="string">'min'</span>: <span class="number">48</span>, <span class="string">'max'</span>: <span class="number">63</span>, <span class="string">'rain'</span>: <span class="number">3.42</span>&#125;,</div><div class="line">        <span class="string">'11'</span>: &#123;<span class="string">'min'</span>: <span class="number">41</span>, <span class="string">'max'</span>: <span class="number">52</span>, <span class="string">'rain'</span>: <span class="number">6.74</span>&#125;,</div><div class="line">        <span class="string">'12'</span>: &#123;<span class="string">'min'</span>: <span class="number">36</span>, <span class="string">'max'</span>: <span class="number">45</span>, <span class="string">'rain'</span>: <span class="number">6.94</span>&#125;</div><div class="line">    &#125;</div><div class="line">	highlight = &#123;<span class="string">'min'</span>: <span class="number">40</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">5</span>&#125;</div><div class="line"></div><div class="line">	<span class="keyword">return</span> render_template(<span class="string">'index.html'</span>, city=city, months=months,</div><div class="line">		weather=weather,highlight=highlight)</div><div class="line"><span class="string">'''</span></div><div class="line"><span class="string">404页面</span></div><div class="line"><span class="string">'''</span></div><div class="line"><span class="meta">@app.errorhandler(404)</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">not_found</span><span class="params">(e)</span>:</span></div><div class="line">    <span class="keyword">return</span> render_template(<span class="string">'404.html'</span>)</div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    app.run(port=<span class="number">1012</span>, debug=<span class="keyword">True</span>)</div></pre></td></tr></table></figure>
<p>base.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line">&#123;% extends 'bootstrap/base.html' %&#125;</div><div class="line"></div><div class="line">&#123;% block navbar %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">nav</span> <span class="attr">class</span>=<span class="string">"navbar navbar-inverse"</span> <span class="attr">role</span>=<span class="string">"navigation"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">"navbar-brand"</span> <span class="attr">href</span>=<span class="string">"#"</span>&gt;</span>Weather<span class="tag">&lt;/<span class="name">a</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span>    </div><div class="line">    <span class="tag">&lt;/<span class="name">nav</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div><div class="line"></div><div class="line">&#123;%- block metas %&#125;</div><div class="line"><span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></div><div class="line">&#123;%- endblock metas %&#125;</div><div class="line"></div><div class="line">&#123;% block content %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">        &#123;% block page_content %&#125;&#123;% endblock %&#125;</div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div></pre></td></tr></table></figure></p>
<p>macros.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line">&#123;% macro weather_cell(value, hl) %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">td&#123;%</span> <span class="attr">if</span> <span class="attr">hl</span> %&#125; <span class="attr">class</span>=<span class="string">"warning"</span>&#123;% <span class="attr">endif</span> %&#125;&gt;</span>&#123;&#123; value &#125;&#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">&#123;% endmacro %&#125;</div><div class="line"></div><div class="line">&#123;% macro weather_row(month, min, max, rain, hl_min, hl_max, hl_rain) %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">td</span>&gt;</span>&#123;&#123; month &#125;&#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">        &#123;&#123; weather_cell(min, min <span class="tag">&lt;<span class="name">=</span> <span class="attr">hl_min</span>) &#125;&#125;</span></div><div class="line"><span class="tag">        &#123;&#123; <span class="attr">weather_cell</span>(<span class="attr">max</span>, <span class="attr">max</span> &gt;</span>= hl_max) &#125;&#125;</div><div class="line">        &#123;&#123; weather_cell(rain, rain &gt;= hl_rain) &#125;&#125;</div><div class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></div><div class="line">&#123;% endmacro %&#125;</div></pre></td></tr></table></figure></p>
<p>index.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line"></div><div class="line">&#123;% extends 'base.html' %&#125;</div><div class="line">&#123;% import 'macros.html' as macros %&#125;</div><div class="line"></div><div class="line">&#123;% block title %&#125;Weather Averages for &#123;&#123; city &#125;&#125;&#123;% endblock %&#125;</div><div class="line"></div><div class="line">&#123;% block page_content %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Weather Averages<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123; city &#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">table</span> <span class="attr">class</span>=<span class="string">"table table-hover"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">tr</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">th</span>&gt;</span>Month<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">th</span>&gt;</span>Min<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">th</span>&gt;</span>Max<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">th</span>&gt;</span>Rainfall<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></div><div class="line">        &#123;% for month in months %&#125;</div><div class="line">            &#123;&#123; macros.weather_row(month,</div><div class="line">                                  weather[month]['min'],</div><div class="line">                                  weather[month]['max'], </div><div class="line">                                  weather[month]['rain'],</div><div class="line">                                  highlight['min'],</div><div class="line">                                  highlight['max'],</div><div class="line">                                  highlight['rain']) &#125;&#125;</div><div class="line">        &#123;% endfor %&#125;</div><div class="line">    <span class="tag">&lt;/<span class="name">table</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div></pre></td></tr></table></figure></p>
<p>404.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line">&#123;% extends 'base.html' %&#125;</div><div class="line"></div><div class="line">&#123;% block title %&#125;Not Found&#123;% endblock %&#125;</div><div class="line"></div><div class="line">&#123;% block page_content %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>404 页面未找到<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div></pre></td></tr></table></figure></p>
<h3 id="url-for-函数"><a href="#url-for-函数" class="headerlink" title="url_for() 函数"></a>url_for() 函数</h3><p>直接跳转到控制层某函数<br>app.py<br><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="comment">#coding:utf-8</span></div><div class="line"></div><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template</div><div class="line"><span class="keyword">from</span> flask_bootstrap <span class="keyword">import</span> Bootstrap</div><div class="line">app = Flask(__name__)</div><div class="line">bootstrap = Bootstrap(app)</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></div><div class="line">	city = <span class="string">u'北京'</span></div><div class="line">	months = [<span class="string">'1'</span>, <span class="string">'2'</span>, <span class="string">'3'</span>, <span class="string">'4'</span>,<span class="string">'5'</span>, <span class="string">'6'</span>, <span class="string">'7'</span>, <span class="string">'8'</span>, <span class="string">'9'</span>, <span class="string">'10'</span>, <span class="string">'11'</span>, <span class="string">'12'</span>]</div><div class="line">	weather = &#123;</div><div class="line">        <span class="string">'1'</span>: &#123;<span class="string">'min'</span>: <span class="number">38</span>, <span class="string">'max'</span>: <span class="number">47</span>, <span class="string">'rain'</span>: <span class="number">6.14</span>&#125;,</div><div class="line">        <span class="string">'2'</span>: &#123;<span class="string">'min'</span>: <span class="number">38</span>, <span class="string">'max'</span>: <span class="number">51</span>, <span class="string">'rain'</span>: <span class="number">4.79</span>&#125;,</div><div class="line">        <span class="string">'3'</span>: &#123;<span class="string">'min'</span>: <span class="number">41</span>, <span class="string">'max'</span>: <span class="number">56</span>, <span class="string">'rain'</span>: <span class="number">4.5</span>&#125;,</div><div class="line">        <span class="string">'4'</span>: &#123;<span class="string">'min'</span>: <span class="number">44</span>, <span class="string">'max'</span>: <span class="number">61</span>, <span class="string">'rain'</span>: <span class="number">3.4</span>&#125;,</div><div class="line">        <span class="string">'5'</span>: &#123;<span class="string">'min'</span>: <span class="number">49</span>, <span class="string">'max'</span>: <span class="number">67</span>, <span class="string">'rain'</span>: <span class="number">2.55</span>&#125;,</div><div class="line">        <span class="string">'6'</span>: &#123;<span class="string">'min'</span>: <span class="number">53</span>, <span class="string">'max'</span>: <span class="number">73</span>, <span class="string">'rain'</span>: <span class="number">1.69</span>&#125;,</div><div class="line">        <span class="string">'7'</span>: &#123;<span class="string">'min'</span>: <span class="number">57</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">0.59</span>&#125;,</div><div class="line">        <span class="string">'8'</span>: &#123;<span class="string">'min'</span>: <span class="number">58</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">0.71</span>&#125;,</div><div class="line">        <span class="string">'9'</span>: &#123;<span class="string">'min'</span>: <span class="number">54</span>, <span class="string">'max'</span>: <span class="number">75</span>, <span class="string">'rain'</span>: <span class="number">1.54</span>&#125;,</div><div class="line">        <span class="string">'10'</span>: &#123;<span class="string">'min'</span>: <span class="number">48</span>, <span class="string">'max'</span>: <span class="number">63</span>, <span class="string">'rain'</span>: <span class="number">3.42</span>&#125;,</div><div class="line">        <span class="string">'11'</span>: &#123;<span class="string">'min'</span>: <span class="number">41</span>, <span class="string">'max'</span>: <span class="number">52</span>, <span class="string">'rain'</span>: <span class="number">6.74</span>&#125;,</div><div class="line">        <span class="string">'12'</span>: &#123;<span class="string">'min'</span>: <span class="number">36</span>, <span class="string">'max'</span>: <span class="number">45</span>, <span class="string">'rain'</span>: <span class="number">6.94</span>&#125;</div><div class="line">    &#125;</div><div class="line">	highlight = &#123;<span class="string">'min'</span>: <span class="number">40</span>, <span class="string">'max'</span>: <span class="number">80</span>, <span class="string">'rain'</span>: <span class="number">5</span>&#125;</div><div class="line"></div><div class="line">	<span class="keyword">return</span> render_template(<span class="string">'index.html'</span>, city=city, months=months,</div><div class="line">		weather=weather,highlight=highlight)</div><div class="line"><span class="string">'''</span></div><div class="line"><span class="string">404页面</span></div><div class="line"><span class="string">'''</span></div><div class="line"><span class="meta">@app.errorhandler(404)</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">not_found</span><span class="params">(e)</span>:</span></div><div class="line">    <span class="keyword">return</span> render_template(<span class="string">'404.html'</span>)</div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    app.run(port=<span class="number">1012</span>, debug=<span class="keyword">True</span>)</div></pre></td></tr></table></figure></p>
<p>index.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line"></div><div class="line"></div><div class="line">&#123;% extends 'bootstrap/base.html' %&#125;</div><div class="line">&#123;% import 'macros.html' as macros %&#125;</div><div class="line"></div><div class="line">&#123;% block title %&#125;Weather Averages for &#123;&#123; city &#125;&#125;&#123;% endblock %&#125;</div><div class="line"></div><div class="line">&#123;% block navbar %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">nav</span> <span class="attr">class</span>=<span class="string">"navbar navbar-inverse"</span> <span class="attr">role</span>=<span class="string">"navigation"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">"navbar-brand"</span> <span class="attr">href</span>=<span class="string">"#"</span>&gt;</span>Weather<span class="tag">&lt;/<span class="name">a</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span>    </div><div class="line">    <span class="tag">&lt;/<span class="name">nav</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div><div class="line"></div><div class="line">&#123;% block content %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Weather Averages<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123; city &#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">table</span> <span class="attr">class</span>=<span class="string">"table table-hover"</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">tr</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">th</span>&gt;</span>Month<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">th</span>&gt;</span>Min<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">th</span>&gt;</span>Max<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">th</span>&gt;</span>Rainfall<span class="tag">&lt;/<span class="name">th</span>&gt;</span></div><div class="line">            <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></div><div class="line">            &#123;% for month in months %&#125;</div><div class="line">                &#123;&#123; macros.weather_row(month,</div><div class="line">                                      weather[month]['min'],</div><div class="line">                                      weather[month]['max'], </div><div class="line">                                      weather[month]['rain'],</div><div class="line">                                      highlight['min'],</div><div class="line">                                      highlight['max'],</div><div class="line">                                      highlight['rain']) &#125;&#125;</div><div class="line">            &#123;% endfor %&#125;</div><div class="line">        <span class="tag">&lt;/<span class="name">table</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span>    </div><div class="line">&#123;% endblock %&#125;</div></pre></td></tr></table></figure></p>
<p>macros.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 定义函数 值与变量 当h1满足条件时加粗字体 --&gt;</span></div><div class="line">&#123;% macro weather_cell(value, hl) %&#125;</div><div class="line">   <span class="tag">&lt;<span class="name">td&#123;%</span> <span class="attr">if</span> <span class="attr">hl</span> %&#125; <span class="attr">class</span>=<span class="string">"warning"</span>&#123;% <span class="attr">endif</span> %&#125;&gt;</span>&#123;&#123; value &#125;&#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">&#123;% endmacro %&#125;</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">&#123;% macro weather_row(month, min, max, rain, hl_min, hl_max, hl_rain) %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">td</span>&gt;</span>&#123;&#123; month &#125;&#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">        &#123;&#123; weather_cell(min, min <span class="tag">&lt;<span class="name">=</span> <span class="attr">hl_min</span>) &#125;&#125;</span></div><div class="line"><span class="tag">        &#123;&#123; <span class="attr">weather_cell</span>(<span class="attr">max</span>, <span class="attr">max</span> &gt;</span>= hl_max) &#125;&#125;</div><div class="line">        &#123;&#123; weather_cell(rain, rain &gt;= hl_rain) &#125;&#125;</div><div class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></div><div class="line">&#123;% endmacro %&#125;</div><div class="line"><span class="comment">&lt;!-- 函数调用函数  当min 低于预定值时会加粗显示--&gt;</span></div><div class="line"></div><div class="line"></div><div class="line">&#123;% macro weather_cell(value, hl) %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">td&#123;%</span> <span class="attr">if</span> <span class="attr">hl</span> %&#125; <span class="attr">class</span>=<span class="string">"warning"</span>&#123;% <span class="attr">endif</span> %&#125;&gt;</span>&#123;&#123; value &#125;&#125;<span class="tag">&lt;/<span class="name">td</span>&gt;</span></div><div class="line">&#123;% endmacro %&#125;</div></pre></td></tr></table></figure></p>
<p>404.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line">&#123;% extends 'base.html' %&#125;</div><div class="line"></div><div class="line">&#123;% block title %&#125;Not Found&#123;% endblock %&#125;</div><div class="line"></div><div class="line">&#123;% block page_content %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>404 页面未找到<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"&#123;&#123; url_for('index') &#125;&#125;"</span>&gt;</span>Return to home page<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div></pre></td></tr></table></figure></p>
<p>base.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line">&#123;% extends 'bootstrap/base.html' %&#125;</div><div class="line"></div><div class="line">&#123;% block navbar %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">nav</span> <span class="attr">class</span>=<span class="string">"navbar navbar-inverse"</span> <span class="attr">role</span>=<span class="string">"navigation"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">"navbar-brand"</span> <span class="attr">href</span>=<span class="string">"&#123;&#123; url_for('index') &#125;&#125;"</span>&gt;</span>Weather<span class="tag">&lt;/<span class="name">a</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span>    </div><div class="line">    <span class="tag">&lt;/<span class="name">nav</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div><div class="line">&#123;%- block metas %&#125;</div><div class="line"><span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></div><div class="line">&#123;%- endblock metas %&#125;</div><div class="line"></div><div class="line">&#123;% block content %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">        &#123;% block page_content %&#125;&#123;% endblock %&#125;</div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div></pre></td></tr></table></figure></p>
<h2 id="request-函数"><a href="#request-函数" class="headerlink" title="request 函数"></a>request 函数</h2><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template, request</div><div class="line"><span class="keyword">from</span> flask_bootstrap <span class="keyword">import</span> Bootstrap</div><div class="line"></div><div class="line">app = Flask(__name__)</div><div class="line">bootstrap = Bootstrap(app)</div><div class="line"></div><div class="line"><span class="meta">@app.route('/', methods=['GET', 'POST'])</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></div><div class="line">    name = <span class="keyword">None</span></div><div class="line">    <span class="keyword">print</span> request.form</div><div class="line">    <span class="keyword">if</span> request.method == <span class="string">'POST'</span> <span class="keyword">and</span> <span class="string">'name'</span> <span class="keyword">in</span> request.form:</div><div class="line">        name = request.form[<span class="string">'name'</span>]</div><div class="line">    <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>, name=name)</div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    app.run(debug=<span class="keyword">True</span>)</div></pre></td></tr></table></figure>
<p>index.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line">&#123;% extends "bootstrap/base.html" %&#125;</div><div class="line"></div><div class="line">&#123;% block title %&#125;Form Example&#123;% endblock %&#125;</div><div class="line"></div><div class="line">&#123;% block navbar %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">nav</span> <span class="attr">class</span>=<span class="string">"navbar navbar-inverse"</span> <span class="attr">role</span>=<span class="string">"navigation"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">"navbar-brand"</span> <span class="attr">href</span>=<span class="string">"&#123;&#123; url_for('index') &#125;&#125;"</span>&gt;</span>Hello<span class="tag">&lt;/<span class="name">a</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">nav</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div><div class="line"></div><div class="line">&#123;% block content %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">form</span> <span class="attr">method</span>=<span class="string">"POST"</span> <span class="attr">action</span>=<span class="string">""</span>&gt;</span></div><div class="line">            What is your name? <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">name</span>=<span class="string">"name"</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"submit"</span> <span class="attr">name</span>=<span class="string">"submit"</span> <span class="attr">value</span>=<span class="string">"Submit"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">form</span>&gt;</span></div><div class="line">        &#123;% if name %&#125;</div><div class="line">            <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Hello, &#123;&#123; name &#125;&#125;!<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">        &#123;% endif %&#125;</div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div></pre></td></tr></table></figure></p>
<h3 id="限定上传长度"><a href="#限定上传长度" class="headerlink" title="限定上传长度"></a>限定上传长度</h3><p>hello.py<br><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template</div><div class="line"><span class="keyword">from</span> flask_bootstrap <span class="keyword">import</span> Bootstrap</div><div class="line"><span class="keyword">from</span> flask_wtf  <span class="keyword">import</span> Form</div><div class="line"></div><div class="line"><span class="keyword">from</span> wtforms <span class="keyword">import</span> StringField, SubmitField</div><div class="line"><span class="keyword">from</span> wtforms.validators <span class="keyword">import</span> Required, Length</div><div class="line"></div><div class="line">app = Flask(__name__)</div><div class="line">app.config[<span class="string">'SECRET_KEY'</span>] = <span class="string">'top secret!'</span></div><div class="line">bootstrap = Bootstrap(app)</div><div class="line"></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">NameForm</span><span class="params">(Form)</span>:</span></div><div class="line">    name = StringField(<span class="string">'What is your name?'</span>, validators=[Required(),</div><div class="line">                                                         Length(<span class="number">1</span>, <span class="number">16</span>)])</div><div class="line">    submit = SubmitField(<span class="string">'Submit'</span>)</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/', methods=['GET', 'POST'])</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></div><div class="line">    name = <span class="keyword">None</span></div><div class="line">    form = NameForm()</div><div class="line">    <span class="keyword">if</span> form.validate_on_submit():</div><div class="line">        name = form.name.data</div><div class="line">        form.name.data = <span class="string">''</span></div><div class="line">    <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>, form=form, name=name)</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    app.run(debug=<span class="keyword">True</span>)</div></pre></td></tr></table></figure></p>
<p>index.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line">&#123;% extends "bootstrap/base.html" %&#125;</div><div class="line"></div><div class="line">&#123;% block title %&#125;Form Example&#123;% endblock %&#125;</div><div class="line"></div><div class="line">&#123;% block navbar %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">nav</span> <span class="attr">class</span>=<span class="string">"navbar navbar-inverse"</span> <span class="attr">role</span>=<span class="string">"navigation"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">"navbar-brand"</span> <span class="attr">href</span>=<span class="string">"&#123;&#123; url_for('index') &#125;&#125;"</span>&gt;</span>Hello<span class="tag">&lt;/<span class="name">a</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span>    </div><div class="line">    <span class="tag">&lt;/<span class="name">nav</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div><div class="line"></div><div class="line">&#123;% block content %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">form</span> <span class="attr">method</span>=<span class="string">"POST"</span> <span class="attr">action</span>=<span class="string">""</span>&gt;</span></div><div class="line">            &#123;&#123; form.name.label &#125;&#125; &#123;&#123; form.name(size=16) &#125;&#125;</div><div class="line">            &#123;% for error in form.name.errors %&#125;</div><div class="line">                &#123;&#123; error &#125;&#125;</div><div class="line">            &#123;% endfor %&#125;</div><div class="line">            <span class="tag">&lt;<span class="name">br</span>&gt;</span></div><div class="line">            &#123;&#123; form.submit() &#125;&#125;</div><div class="line">            &#123;&#123; form.hidden_tag() &#125;&#125;</div><div class="line">        <span class="tag">&lt;/<span class="name">form</span>&gt;</span></div><div class="line">        &#123;% if name %&#125;</div><div class="line">            <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Hello, &#123;&#123; name &#125;&#125;!<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">        &#123;% endif %&#125;</div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div></pre></td></tr></table></figure></p>
<h3 id="bootstrap美化表单"><a href="#bootstrap美化表单" class="headerlink" title="bootstrap美化表单"></a>bootstrap美化表单</h3><p>hello.py<br><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template</div><div class="line"><span class="keyword">from</span> flask_bootstrap <span class="keyword">import</span> Bootstrap</div><div class="line"><span class="keyword">from</span> flask_wtf  <span class="keyword">import</span> Form</div><div class="line"></div><div class="line"><span class="keyword">from</span> wtforms <span class="keyword">import</span> StringField, SubmitField</div><div class="line"><span class="keyword">from</span> wtforms.validators <span class="keyword">import</span> Required, Length</div><div class="line"></div><div class="line">app = Flask(__name__)</div><div class="line">app.config[<span class="string">'SECRET_KEY'</span>] = <span class="string">'top secret!'</span></div><div class="line">bootstrap = Bootstrap(app)</div><div class="line"></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">NameForm</span><span class="params">(Form)</span>:</span></div><div class="line">    name = StringField(<span class="string">'What is your name?'</span>, validators=[Required(),</div><div class="line">                                                         Length(<span class="number">1</span>, <span class="number">16</span>)])</div><div class="line">    submit = SubmitField(<span class="string">'Submit'</span>)</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/', methods=['GET', 'POST'])</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></div><div class="line">    name = <span class="keyword">None</span></div><div class="line">    form = NameForm()</div><div class="line">    <span class="keyword">if</span> form.validate_on_submit():</div><div class="line">        name = form.name.data</div><div class="line">        form.name.data = <span class="string">''</span></div><div class="line">    <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>, form=form, name=name)</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    app.run(debug=<span class="keyword">True</span>)</div></pre></td></tr></table></figure></p>
<p>index.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line"></div><div class="line">&#123;% extends "bootstrap/base.html" %&#125;</div><div class="line">&#123;% import "bootstrap/wtf.html" as wtf %&#125;</div><div class="line"></div><div class="line">&#123;% block title %&#125;Form Example&#123;% endblock %&#125;</div><div class="line"></div><div class="line">&#123;% block navbar %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">nav</span> <span class="attr">class</span>=<span class="string">"navbar navbar-inverse"</span> <span class="attr">role</span>=<span class="string">"navigation"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">"navbar-brand"</span> <span class="attr">href</span>=<span class="string">"&#123;&#123; url_for('index') &#125;&#125;"</span>&gt;</span>Hello<span class="tag">&lt;/<span class="name">a</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">nav</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div><div class="line"></div><div class="line">&#123;% block content %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"row"</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"col-md-3"</span>&gt;</span></div><div class="line">                &#123;&#123; wtf.quick_form(form) &#125;&#125;</div><div class="line">                &#123;% if name %&#125;</div><div class="line">                    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Hello, &#123;&#123; name &#125;&#125;!<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">                &#123;% endif %&#125;</div><div class="line">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div></pre></td></tr></table></figure></p>
<h3 id="文件上传"><a href="#文件上传" class="headerlink" title="文件上传"></a>文件上传</h3><p>├── static<br>│   └── uploads<br>│       └── README.md 文件会上传到此目录<br>├── templates<br>│   └── index.html<br>└── upload.py</p>
<p>3 directories, 3 files</p>
<p>upload.py<br><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="keyword">import</span> os</div><div class="line"><span class="keyword">import</span> imghdr</div><div class="line"></div><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template</div><div class="line"><span class="keyword">from</span> flask_bootstrap <span class="keyword">import</span> Bootstrap</div><div class="line"><span class="keyword">from</span> flask_wtf  <span class="keyword">import</span> Form</div><div class="line"><span class="keyword">from</span> wtforms <span class="keyword">import</span> FileField, SubmitField, ValidationError</div><div class="line"></div><div class="line">app = Flask(__name__)</div><div class="line">app.config[<span class="string">'SECRET_KEY'</span>] = <span class="string">'top secret!'</span></div><div class="line">bootstrap = Bootstrap(app)</div><div class="line"></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">UploadForm</span><span class="params">(Form)</span>:</span></div><div class="line">    image_file = FileField(<span class="string">'Image file'</span>)</div><div class="line">    submit = SubmitField(<span class="string">'Submit'</span>)</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">validate_image_file</span><span class="params">(self, field)</span>:</span></div><div class="line">        <span class="keyword">if</span> field.data.filename[<span class="number">-4</span>:].lower() != <span class="string">'.jpg'</span>:</div><div class="line">            <span class="keyword">raise</span> ValidationError(<span class="string">'Invalid file extension'</span>)</div><div class="line">        <span class="keyword">if</span> imghdr.what(field.data) != <span class="string">'jpeg'</span>:</div><div class="line">            <span class="keyword">raise</span> ValidationError(<span class="string">'Invalid image format'</span>)</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/', methods=['GET', 'POST'])</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></div><div class="line">    image = <span class="keyword">None</span></div><div class="line">    form = UploadForm()</div><div class="line">    <span class="keyword">if</span> form.validate_on_submit():</div><div class="line">        image = <span class="string">'uploads/'</span> + form.image_file.data.filename</div><div class="line">        form.image_file.data.save(os.path.join(app.static_folder, image))</div><div class="line">    <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>, form=form, image=image)</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    app.run(debug=<span class="keyword">True</span>)</div></pre></td></tr></table></figure></p>
<figure class="highlight html"><table><tr><td class="code"><pre><div class="line">&#123;% extends "bootstrap/base.html" %&#125;</div><div class="line">&#123;% import "bootstrap/wtf.html" as wtf %&#125;</div><div class="line"></div><div class="line">&#123;% block title %&#125;File Upload Example&#123;% endblock %&#125;</div><div class="line"></div><div class="line">&#123;% block navbar %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">nav</span> <span class="attr">class</span>=<span class="string">"navbar navbar-inverse"</span> <span class="attr">role</span>=<span class="string">"navigation"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">"navbar-brand"</span> <span class="attr">href</span>=<span class="string">"&#123;&#123; url_for('index') &#125;&#125;"</span>&gt;</span>Upload<span class="tag">&lt;/<span class="name">a</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">nav</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div><div class="line"></div><div class="line">&#123;% block content %&#125;</div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"row"</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"col-md-4"</span>&gt;</span></div><div class="line">                &#123;&#123; wtf.quick_form(form, enctype='multipart/form-data') &#125;&#125;</div><div class="line">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">        &#123;% if image %&#125;</div><div class="line">            <span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">"&#123;&#123; url_for('static', filename=image) &#125;&#125;"</span>&gt;</span></div><div class="line">        &#123;% endif %&#125;</div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">&#123;% endblock %&#125;</div></pre></td></tr></table></figure>
<h2 id="session-信息"><a href="#session-信息" class="headerlink" title="session 信息"></a>session 信息</h2><p>plus.py<br><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template, session</div><div class="line">app = Flask(__name__)</div><div class="line">app.config[<span class="string">'SECRET_KEY'</span>] = <span class="string">'top secret!'</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">print</span> session</div><div class="line">    <span class="keyword">if</span> <span class="string">'count'</span> <span class="keyword">not</span> <span class="keyword">in</span> session:</div><div class="line">        session[<span class="string">'count'</span>] = <span class="number">1</span></div><div class="line">    <span class="keyword">else</span>:</div><div class="line">        session[<span class="string">'count'</span>] += <span class="number">1</span></div><div class="line">    <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>, count=session[<span class="string">'count'</span>])</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    app.run(debug=<span class="keyword">True</span>)</div></pre></td></tr></table></figure></p>
<p>index.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>You have seen this page &#123;&#123; count &#125;&#125; times.<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span>Hit refresh to increase.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h3 id="页面访问统计-以及时间"><a href="#页面访问统计-以及时间" class="headerlink" title="页面访问统计 以及时间"></a>页面访问统计 以及时间</h3><p>plus1.py<br><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="keyword">from</span> datetime <span class="keyword">import</span> datetime</div><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template, session, g</div><div class="line">app = Flask(__name__)</div><div class="line">app.config[<span class="string">'SECRET_KEY'</span>] = <span class="string">'top secret!'</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.before_request</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">before_request</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">if</span> <span class="keyword">not</span> <span class="string">'count'</span> <span class="keyword">in</span> session:</div><div class="line">        session[<span class="string">'count'</span>] = <span class="number">1</span></div><div class="line">    <span class="keyword">else</span>:</div><div class="line">        session[<span class="string">'count'</span>] += <span class="number">1</span></div><div class="line">    g.when = datetime.now().strftime(<span class="string">'%H:%M:%S'</span>)</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>, count=session[<span class="string">'count'</span>], when=g.when)</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/other')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">other</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">return</span> render_template(<span class="string">'other.html'</span>, count=session[<span class="string">'count'</span>], when=g.when)</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    app.run(debug=<span class="keyword">True</span>)</div></pre></td></tr></table></figure></p>
<p>index.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>Index Page<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span>You have a total of &#123;&#123; count &#125;&#125; page views in this site, the last of which was at &#123;&#123; when &#125;&#125;.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"&#123;&#123; url_for('other') &#125;&#125;"</span>&gt;</span>Click here<span class="tag">&lt;/<span class="name">a</span>&gt;</span> to see the other page.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>other.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>Other Page<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span>You have a total of &#123;&#123; count &#125;&#125; page views in this site, the last of which was at &#123;&#123; when &#125;&#125;.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"&#123;&#123; url_for('index') &#125;&#125;"</span>&gt;</span>Click here<span class="tag">&lt;/<span class="name">a</span>&gt;</span> to see the index page.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h3 id="多种请求-响应方式"><a href="#多种请求-响应方式" class="headerlink" title="多种请求 响应方式"></a>多种请求 响应方式</h3><p>responses.py<br><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template, make_response, jsonify, redirect, \</div><div class="line">    url_for</div><div class="line">app = Flask(__name__)</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>)</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/text')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">text</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">return</span> render_template(<span class="string">'text.txt'</span>), <span class="number">200</span>, &#123;<span class="string">'Content-Type'</span>: <span class="string">'text/plain'</span>&#125;</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/xml')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">xml</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">return</span> <span class="string">'&lt;h1&gt;this is shown as &lt;b&gt;XML&lt;/b&gt; in the browser&lt;/h1&gt;'</span>, <span class="number">200</span>, \</div><div class="line">           &#123;<span class="string">'Content-Type'</span>: <span class="string">'application/xml'</span>&#125;</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/json')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">json</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">return</span> jsonify(&#123;<span class="string">'first_name'</span>: <span class="string">'John'</span>, <span class="string">'last_name'</span>: <span class="string">'Smith'</span>&#125;)</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/redirect')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">redir</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">return</span> redirect(url_for(<span class="string">'text'</span>))</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/cookie')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">cookie</span><span class="params">()</span>:</span></div><div class="line">    resp = redirect(url_for(<span class="string">'index'</span>))</div><div class="line">    resp.set_cookie(<span class="string">'cookie'</span>, <span class="string">'Hello, I\'m a cookie'</span>)</div><div class="line">    <span class="keyword">return</span> resp</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/error')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">error</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">return</span> <span class="string">'Bad Request'</span>, <span class="number">400</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/response')</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">response</span><span class="params">()</span>:</span></div><div class="line">    resp = make_response(render_template(<span class="string">'text.txt'</span>))</div><div class="line">    resp.headers[<span class="string">'Content-Type'</span>] = <span class="string">'text/plain'</span></div><div class="line">    <span class="keyword">return</span> resp</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    app.run(debug=<span class="keyword">True</span>)</div></pre></td></tr></table></figure></p>
<p>text.text<br><figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">This page contains plain text.</div><div class="line">Use your back button to go back to the index page.</div></pre></td></tr></table></figure></p>
<p>index.html<br><figure class="highlight html"><table><tr><td class="code"><pre><div class="line"></div><div class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>This page contains HTML<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span></div><div class="line">    Cookie: </div><div class="line">    &#123;% if 'cookie' in request.cookies %&#125;</div><div class="line">        <span class="tag">&lt;<span class="name">b</span>&gt;</span>&#123;&#123; request.cookies['cookie'] &#125;&#125;<span class="tag">&lt;/<span class="name">b</span>&gt;</span></div><div class="line">    &#123;% else %&#125;</div><div class="line">        not set</div><div class="line">    &#123;% endif %&#125;</div><div class="line"><span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"&#123;&#123; url_for('text') &#125;&#125;"</span>&gt;</span>Click here<span class="tag">&lt;/<span class="name">a</span>&gt;</span> to view a page that contains plain text.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"&#123;&#123; url_for('xml') &#125;&#125;"</span>&gt;</span>Click here<span class="tag">&lt;/<span class="name">a</span>&gt;</span> to view a page that contains XML data.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"&#123;&#123; url_for('json') &#125;&#125;"</span>&gt;</span>Click here<span class="tag">&lt;/<span class="name">a</span>&gt;</span> to view a page that contains JSON data.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"&#123;&#123; url_for('redir') &#125;&#125;"</span>&gt;</span>Click here<span class="tag">&lt;/<span class="name">a</span>&gt;</span> to view a page that redirects to the plain text page.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"&#123;&#123; url_for('cookie') &#125;&#125;"</span>&gt;</span>Click here<span class="tag">&lt;/<span class="name">a</span>&gt;</span> to view a page that sets a cookie and then redirects back to this page.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"&#123;&#123; url_for('response') &#125;&#125;"</span>&gt;</span>Click here<span class="tag">&lt;/<span class="name">a</span>&gt;</span> to view a page that shows plain text through a response object.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h3 id="from表单取值"><a href="#from表单取值" class="headerlink" title="from表单取值"></a>from表单取值</h3><p><strong>复选框</strong><br><figure class="highlight python"><table><tr><td class="code"><pre><div class="line">&lt;div class="form-group"&gt;</div><div class="line">    &lt;label class="control-label col-sm-2 col-lg-2 text-navy" for="id_title"&gt;操作系统&lt;/label&gt;</div><div class="line">        &lt;div class="col-sm-10"&gt;</div><div class="line">            &lt;input type=<span class="string">"checkbox"</span> name=<span class="string">"hobby"</span> value=<span class="string">"football"</span>/&gt;足球</div><div class="line">            &lt;input type=<span class="string">"checkbox"</span> name=<span class="string">"hobby"</span> value=<span class="string">"basketball"</span> checked=<span class="string">"checked"</span>/&gt;篮球</div><div class="line">            &lt;input type=<span class="string">"checkbox"</span> name=<span class="string">"hobby"</span> value=<span class="string">"pingpong"</span> checked=<span class="string">"checked"</span> /&gt;乒乓球</div><div class="line">        &lt;/div&gt;</div><div class="line">&lt;/div&gt;</div><div class="line"></div><div class="line">-----</div><div class="line"><span class="meta">@app.route('/aa', methods=['POST'])</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">assts_aa</span><span class="params">()</span>:</span></div><div class="line">    tmp = request.values.getlist(<span class="string">'hobby'</span>)</div><div class="line">    <span class="keyword">print</span> tmp</div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> tmp</div><div class="line">    	<span class="keyword">pass</span></div><div class="line">    get_id = <span class="number">1</span></div></pre></td></tr></table></figure></p>
<p><strong>单选框</strong></p>
<figure class="highlight python"><table><tr><td class="code"><pre><div class="line">&lt;div class="form-group"&gt;</div><div class="line">&lt;label class="control-label col-sm-2 col-lg-2 text-navy" for="id_title"&gt;操作系统&lt;/label&gt;</div><div class="line">    &lt;div class="col-sm-10"&gt;</div><div class="line">         &lt;input type=<span class="string">"radio"</span> name=<span class="string">"sex"</span> value=<span class="string">"1"</span> checked=<span class="string">"checked"</span> /&gt;男</div><div class="line">         &lt;input type=<span class="string">"radio"</span> name=<span class="string">"sex"</span> value=<span class="string">"0"</span> /&gt;女</div><div class="line">    &lt;/div&gt;</div><div class="line">&lt;/div&gt;</div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">@app.route('/aa', methods=['POST'])</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">assts_aa</span><span class="params">()</span>:</span></div><div class="line">    <span class="keyword">print</span> request.form.get(<span class="string">'sex'</span>,<span class="string">''</span>)</div></pre></td></tr></table></figure>
<h3 id="flask获取上传表单-列表形式"><a href="#flask获取上传表单-列表形式" class="headerlink" title="flask获取上传表单(列表形式)"></a>flask获取上传表单(列表形式)</h3><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="comment"># html内容</span></div><div class="line">&lt;div class="form-group"&gt;</div><div class="line">    &lt;label class="col-sm-2 control-label"&gt;项目组:&lt;/label&gt;</div><div class="line">    &lt;div class="col-sm-10"&gt;</div><div class="line">        &lt;! -- start--&gt;</div><div class="line">        &#123;% <span class="keyword">if</span> project_group_list %&#125;</div><div class="line">            &#123;% <span class="keyword">for</span> foo <span class="keyword">in</span> project_group_list %&#125;</div><div class="line">                &lt;input id=<span class="string">"id_title"</span> type=<span class="string">"checkbox"</span>  name=<span class="string">"project_group_name"</span></div><div class="line">                           value=<span class="string">"&#123;&#123; foo['id'] &#125;&#125;"</span>/&gt;&#123;&#123; foo[<span class="string">'project_group_name'</span>] &#125;&#125;&amp;nbsp&amp;nbsp</div><div class="line">            &#123;% endfor %&#125;</div><div class="line"></div><div class="line">        &#123;% endif %&#125;</div><div class="line">        &lt;! -- end--&gt;</div><div class="line">    &lt;/div&gt;</div><div class="line">&lt;/div&gt;</div><div class="line"></div><div class="line"><span class="comment"># 上传的表单数据</span></div><div class="line">project_group_name:<span class="number">1</span></div><div class="line">project_group_name:<span class="number">2</span></div><div class="line">project_group_name:<span class="number">3</span></div><div class="line">project_group_name:<span class="number">4</span></div><div class="line">project_group_name:<span class="number">5</span></div><div class="line"></div><div class="line"><span class="comment"># python flask获取列表</span></div><div class="line">project_group_name = request.values.getlist(<span class="string">'project_group_name'</span>)</div><div class="line"><span class="keyword">print</span> projects_id</div><div class="line"></div><div class="line"><span class="comment"># 返回值</span></div><div class="line">[<span class="string">u'1'</span>, <span class="string">u'2'</span>, <span class="string">u'3'</span>, <span class="string">u'4'</span>, <span class="string">u'5'</span>]</div><div class="line"></div><div class="line"><span class="comment"># 传给models进行处理</span></div><div class="line">status, result= models.project_id_project_group_id_correlation(project_group_name_id_list_s, project_name)</div><div class="line"><span class="keyword">if</span> <span class="keyword">not</span> status:</div><div class="line">    <span class="keyword">return</span> json.dumps(&#123;<span class="string">'code'</span>: <span class="number">400</span>, <span class="string">'result'</span>: result&#125;)</div><div class="line"><span class="keyword">return</span> json.dumps(&#123;<span class="string">'code'</span>: <span class="number">200</span>, <span class="string">'result'</span>: result&#125;)</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment"># models代码</span></div><div class="line"><span class="string">'''</span></div><div class="line"><span class="string">项目ID.项目组ID关联关系.保存</span></div><div class="line"><span class="string">'''</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">project_id_project_group_id_correlation</span><span class="params">(project_group_name_id_list_s, project_name)</span>:</span></div><div class="line">    SQL_GET_PROJECT_NAME_ID = <span class="string">'''</span></div><div class="line"><span class="string">    SELECT `id` FROM project WHERE `project_name`=%s</span></div><div class="line"><span class="string">    '''</span></div><div class="line">    sql = SQL_GET_PROJECT_NAME_ID</div><div class="line">    args =(project_name,)</div><div class="line">    rt_cnt , rt_list = dbutils.execute_sql(sql, args, <span class="keyword">True</span>)</div><div class="line">    <span class="comment"># 获取刚添加项目ID</span></div><div class="line">    project_id =  rt_list[<span class="number">0</span>][<span class="number">0</span>]</div><div class="line"></div><div class="line">    SQL_PROJECT_ID_PROJECT_GROUP_ID_CORRELATION=<span class="string">'''</span></div><div class="line"><span class="string">    INSERT INTO `project_id_project_group_id` (`project_id`, `project_group_id`) VALUES</span></div><div class="line"><span class="string">    '''</span></div><div class="line">    sql_insert = SQL_PROJECT_ID_PROJECT_GROUP_ID_CORRELATION</div><div class="line">    cnt = <span class="number">0</span></div><div class="line">    <span class="keyword">for</span> project_group_name_id_list <span class="keyword">in</span> project_group_name_id_list_s:</div><div class="line">        <span class="keyword">if</span> cnt == <span class="number">0</span>:</div><div class="line">            sql_insert = sql_insert + <span class="string">'(&#123;project_id&#125;,&#123;project_group_name_id_list&#125;)'</span>.format(project_id=project_id,project_group_name_id_list=project_group_name_id_list)</div><div class="line">        sql_insert = sql_insert + <span class="string">' ,(&#123;project_id&#125;,&#123;project_group_name_id_list&#125;)'</span>.format(project_id=project_id,project_group_name_id_list=project_group_name_id_list)</div><div class="line">        cnt = <span class="number">1</span></div><div class="line">    args = ()</div><div class="line">    <span class="keyword">try</span>:</div><div class="line">        rt_cnt, rt_list = dbutils.execute_sql(sql_insert, args, <span class="keyword">False</span>)</div><div class="line">    <span class="keyword">except</span>:</div><div class="line">        <span class="keyword">return</span> (<span class="keyword">False</span>,<span class="string">u'保存项目id与项目组id关联失败'</span>)</div><div class="line">    <span class="keyword">return</span> (<span class="keyword">True</span>, <span class="string">u'保存项目id与项目组id关联成功'</span>)</div></pre></td></tr></table></figure>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-09-08T07:02:37.038Z" itemprop="dateUpdated">2017-09-08 15:02:37</time>
</span><br>


        
        <a href="/2016/11/29/12-flask/" target="_blank" rel="external">http://zhourudong.cn/2016/11/29/12-flask/</a>
        
    </div>
    <footer>
        <a href="http://zhourudong.cn">
            <img src="/img/avatar.png" alt="cucy">
            cucy
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/flask/">flask</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://zhourudong.cn/2016/11/29/12-flask/&title=《flask》 — Cucy的博客&pic=http://zhourudong.cn/img/avatar.png" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://zhourudong.cn/2016/11/29/12-flask/&title=《flask》 — Cucy的博客&source=flask" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://zhourudong.cn/2016/11/29/12-flask/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《flask》 — Cucy的博客&url=http://zhourudong.cn/2016/11/29/12-flask/&via=http://zhourudong.cn" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://zhourudong.cn/2016/11/29/12-flask/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2016/11/29/13-python-modules/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">python modules</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2016/11/18/11-算法复杂度/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">算法复杂度</h4>
      </a>
    </div>
  
</nav>



    














</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/img/wechat.jpg" data-alipay="/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>cucy &copy; 2016 - 2017</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://zhourudong.cn/2016/11/29/12-flask/&title=《flask》 — Cucy的博客&pic=http://zhourudong.cn/img/avatar.png" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://zhourudong.cn/2016/11/29/12-flask/&title=《flask》 — Cucy的博客&source=flask" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://zhourudong.cn/2016/11/29/12-flask/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《flask》 — Cucy的博客&url=http://zhourudong.cn/2016/11/29/12-flask/&via=http://zhourudong.cn" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://zhourudong.cn/2016/11/29/12-flask/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://zhourudong.cn/2016/11/29/12-flask/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="/js/main.min.js?v=1.6.13"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.6.13" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
